<template>
    <div class="reportFormPage">
        <div id="domid" class="report-dom">
            <el-row>
                <el-col :span="5" :offset="19">
                    <div class="pageNumBox">
                        <span>第</span>
                        <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
                        <span>页，共</span>
                        <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
                        <span>页</span>
                    </div>
                </el-col>
            </el-row>
            <div class="topTwoBox">
                <div class="centerName">岩石含水率试验检测记录表</div>
                <div class="codeText">BGLQ05002F</div>
            </div>
            <div class="threeTopBox">
                <div class="left-border">
                    <div class="box-title">检测单位名称：</div>
                    <div class>{{ form.jiancdwmc }}</div>
                </div>
                <div class="right-border">
                    <span>记录编号：</span>
                    <span class="placeholder">{{ form.jilbh }}</span>
                </div>
            </div>
            <!-- .........................1111111111111111111111111111111......................................................... -->
            <div class="table-border">
                <table height="220px" class="top-table">
                    <tbody>
                    <tr>
                        <td align="center" width="15%" height="25">
                            <span style="width:90px;display:inline-block">工程名称</span>
                        </td>
                        <td width="35%" class="text-center">
                            <span>{{ form.gongcmc }}</span>
                        </td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">工程部位/用途</span>
                        </td>
                        <td width="35%" class="text-center">
                            <span>{{ form.gongcbwyt }}</span>
                        </td>
                    </tr>
                    <tr>
                        <td height="50" align="center">样品信息</td>
                        <td class="specimen-border" colspan="3">
                            <span>样品名称：</span>
                            <span :class="{ 'placeholder': !form.yangpmc }">{{ form.yangpmc }}</span>
                            <span>；样品编号：</span>
                            <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>
                            <span>；样品数量：</span>
                            <span :class="{ 'placeholder': !form.yangpsl }">{{ form.yangpsl }}</span>
                            <span>；样品状态：</span>
                            <span :class="{ 'placeholder': !form.yangpzt }">{{ form.yangpzt }}</span>
                            <span>；来样时间：</span>
                            <span :class="{ 'placeholder': !form.yangpsj }">{{ form.yangpsj }}</span>
                        </td>
                    </tr>
                    <tr>
                        <td height="25" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">试验检测日期</span>
                        </td>
                         <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->                                            
 <td width="35%" class="text-center">
      <el-input v-model="form.shiyjcrq" />
</td>

                        <td align="center" style="width:15%">
                            <span style="width: 90px; display: inline-block">试验条件</span>
                        </td>
                        <td v-if="isPdf" width="35%">
                            温度：
                            <span style="display: inline-block; width: 10%;">{{ form.shiytjwd }}</span>
                            ℃；相对湿度：
                            <span style="display: inline-block; width: 10%;">{{ form.shiytjsd }}</span>%RH
                        </td>
                        <td v-else width="35%">
                            温度：
                            <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjwd" style="width: 15%"/>
                            ℃；相对湿度：
                            <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjsd" style="width: 15%"/>
                            %RH
                        </td>
                    </tr>
                    <tr>
                        <td height="50" align="center" width="15%">
                            <span style="width: 90px; display: inline-block">检测依据</span>
                        </td>
                        <td width="35%">
                            <span v-if="isPdf">{{ form.jiancyj }}</span>
                            <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark"
                                      @dblclick.native="showBsJiancyjView" readonly/>
                        </td>
                        <td align="center" width="15%">
                            <span style="width: 90px; display: inline-block">判定依据</span>
                        </td>
                        <td width="35%">
                            <span v-if="isPdf">{{ form.pandyj }}</span>
                            <el-input v-else type="textarea" v-model="form.pandyj" :rows="2"
                                      @dblclick.native="showBsJudgeBaseView"
                                      @focus="handleFocus('pandyj')" class="mark"/>
                        </td>
                    </tr>
                    <tr class="bottom-border">
                        <td height="50" align="center">
                            <div>主要仪器设备</div>
                            <div>名称及编号</div>
                        </td>
                        <td colspan="3">
                            <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                            <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh"
                                      class="height100 width100 mark"
                                      @dblclick.native="showBsInstrumentView" readonly></el-input>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!-- .................................22222222222222222222222222222222............................................... -->
                <table height="540px" class="middle-table">
                    <tbody>
                    <tr trindex="0">
                        <td width="12%" height="25">编号</td>
                        <td width="20%">称量盒干燥质量(g)</td>
                        <td width="20%">烘干前试样和称量盒质量(g)</td>
                        <td width="20%">烘干后试样和称量盒质量(g)</td>
                        <td width="13%">含水率(%)</td>
                        <td width="15%">含水率平均值(%)</td>
                    </tr>
                    <tr trindex="item.index" style="height:20px" v-for="(item, index) in form.detailList" :key="index">
                        <td width="12%" height="25">{{ index + 1 }}</td>
                        <td width="20%">
                            <el-input type="number"
                                      v-model="item.chenglhgzzl"
                                      @change="handlehsl(item)"
                                      @keydown.native="handleKeyDown($event, index, 0)"
                                      @paste.native="handlePasteDetailList($event,  index, 0)"
                                      :ref="`${index}_0`"></el-input>
                        </td>
                        <td width="20%">
                            <el-input type="number"
                                      v-model="item.honggqsyhclhzl"
                                      @change="handlehsl(item)"
                                      @keydown.native="handleKeyDown($event, index, 1)"
                                      @paste.native="handlePasteDetailList($event,  index, 1)"
                                      :ref="`${index}_1`"></el-input>
                        </td>
                        <td width="20%">
                            <el-input type="number"
                                      v-model="item.hongghsyhclhzl"
                                      @change="handlehsl(item)"
                                      @keydown.native="handleKeyDown($event, index, 2)"
                                      @paste.native="handlePasteDetailList($event,  index, 2)"
                                      :ref="`${index}_2`"></el-input>
                        </td>
                        <td width="13%">{{ item.hansl }}</td>
                        <td width="15%" v-if="index == 0" rowspan="6">{{ form.hanslpjz }}</td>
                    </tr>
                    </tbody>
                </table>
                <!-- .....................................................33333333333333333................................................... -->
                <table height="120px" class="bottom-table">
                    <tr>
                        <td>
                            <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
                            <el-input v-else type="textarea" :rows="2" v-model="form.fujsm"
                                      @focus="handleFocus('fujsm')"/>
                        </td>
                    </tr>
                </table>
            </div>
            <!-- .................................44444444444............................................. -->
            <table height="22px" class="transparent">
                <tbody>
                <tr>
                    <td width="10%" align="right">检测：</td>
                    <td width="15%" align="left"></td>
                    <td width="10%" align="right">记录：</td>
                    <td width="15%" align="left"></td>
                    <td width="10%" align="right">复核：</td>
                    <td align="left" width="15%"></td>
                    <td width="10%">日期：</td>
                    <td align="left" width="15%">
                        <div style="display: inline-block; margin-left: 20px">年</div>
                        <div style="display: inline-block; margin-left: 20px">月</div>
                        <div style="display: inline-block ;margin-left: 20px">日</div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
        <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
        <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
    </div>
</template>

<script>
import {handleFocus, emptyConvert, handleKeyDown, handlePaste} from "@/views/reportForms/utils";
import Bus from "@/utils/bus";
import decimal from "@/utils/big-decimal";
import {getYsYshslsyjcjlbBsInfo} from "@/api/reportys/ysYshslsyjcjlbBsInfo";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";

export default {
    name: "yshslsyjcjlb",
    components: {
        BsInstrumentView,
        BsJiancyjView,
        BsJudgeBaseView
    },
    props: {
        excelId: {
            type: [String, Number],
            default: () => ""
        },
        syType: {
            type: [String, Number],
            default: () => ""
        },
        page: {
            type: [String, Number],
            default: () => ""
        },
        isPdf: {
            type: Boolean,
            default: () => false
        },
        printEmpty: {
            type: Boolean,
            default: () => false
        }
    },
    data() {
        return {
            jilbh: "",
            hanslpjz: "",
            list: [
                {index: 1, chenglhgzzl: "", honggqsyhclhzl: "", hongghsyhclhzl: "", hansl: "", hanslpjz: ""},
                {index: 2, chenglhgzzl: "", honggqsyhclhzl: "", hongghsyhclhzl: "", hansl: "", hanslpjz: ""},
                {index: 3, chenglhgzzl: "", honggqsyhclhzl: "", hongghsyhclhzl: "", hansl: "", hanslpjz: ""},
                {index: 4, chenglhgzzl: "", honggqsyhclhzl: "", hongghsyhclhzl: "", hansl: "", hanslpjz: ""},
                {index: 5, chenglhgzzl: "", honggqsyhclhzl: "", hongghsyhclhzl: "", hansl: "", hanslpjz: ""},
                {index: 6, chenglhgzzl: "", honggqsyhclhzl: "", hongghsyhclhzl: "", hansl: "", hanslpjz: ""}
            ],
            form: {
                detailList: [{}, {}, {}, {}, {}]
            },
            twoTableData: {},
            readonly: false
        };
    },
    watch: {
        isPdf(newVal, oldVal) {
            this.form = {...emptyConvert(this.form, newVal)};
        }
    },
    created() {
        this.getInfo();
    },
    methods: {
        // 获取详情
        getInfo() {
            getYsYshslsyjcjlbBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
                this.form = response.data;
                if (!this.form.fujsm) {
                    this.form.fujsm = "附加声明：";
                }
                this.form = response.data;
                if (this.form.detailList) {
                    this.list = this.form.detailList;
                }
            });
        },
        // 显示检测依据列表
        showBsJiancyjView() {
            this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
        },
        // 检测依据选择
        selectJiancyj(info) {
            this.form.jiancyj = info.label;
            this.form.jiancyjIds = info.ids;
            Bus.$emit("setYansBgJiancyj", {jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds});
        },
        // 显示判定依据列表
        showBsJudgeBaseView() {
            this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
        },
        // 判断依据选择
        selectJudge(info) {
            if (!this.form.pandyj) {
                this.form.pandyj = info.label;
                return;
            }
            if (info && info.label) {
                this.form.pandyj += "、" + info.label;
            }
        },
        // 显示仪器列表
        showBsInstrumentView() {
            this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
        },
        // 仪器选择
        selectInstrument(info) {
            this.form.zhuyyqsbmcjbh = info.label;
            this.form.shebIds = info.ids;
            Bus.$emit("setYansBgZhuyyqsbmcjbh", {zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds});
        },
        // 计算含水率
        handlehsl(item) {
            if (!item.chenglhgzzl || !item.honggqsyhclhzl || !item.hongghsyhclhzl) {
                item.hansl = null;
                this.handlehslpj();
                return null;
            }
            // D=(b-c)/(b-a)
            item.hansl = decimal.divide(decimal.multiply(decimal.subtract(item.honggqsyhclhzl, item.hongghsyhclhzl), 100), decimal.subtract(item.honggqsyhclhzl, item.chenglhgzzl));
            // return item.hansl;
            this.handlehslpj();
        },
        // 计算平均含水率
        handlehslpj() {
            let count = 0;
            let sum = 0;
            if (this.form.detailList && this.form.detailList.length > 0) {
                this.form.detailList.forEach((item, index) => {
                    if (item.hansl) {
                        count++;
                        sum = decimal.add(sum, item.hansl);
                    }
                });
                if (count > 0) {
                    this.form.detailList[0].hanslpjz = decimal.divide(sum, count, 2);
                    this.form.hanslpjz = this.form.detailList[0].hanslpjz;
                    this.setTgBgHansl();
                    return this.form.hanslpjz;
                } else {
                    this.form.hanslpjz = null;
                }
                return "";
            }
        },
        // 给报告传值含水率
        setTgBgHansl() {
            if (this.page != 0 || !this.form.hanslpjz) {
                return;
            }
            Bus.$emit("setYansBgJiancjg", [{key: 154, value: this.form.hanslpjz}]);
        },
        submitForm() {
            // this.form.detailList = [];
            // this.list.forEach((item) => {
            // if (item.chenglhgzzl || item.honggqsyhclhzl || item.hongghsyhclhzl) {
            //   let e = {
            //     chenglhgzzl: item.chenglhgzzl,
            //     honggqsyhclhzl: item.honggqsyhclhzl,
            //     hongghsyhclhzl: item.hongghsyhclhzl,
            //     hansl: item.hansl,
            //     hanslpjz: this.hanslpjz
            //   };
            //   this.form.detailList.push(e);
            // }
            // });
            return this.form;
        },
        handleFocus(event) {
            handleFocus(event, this.page, this.form[event]);
        },
        handleKeyDown(event, rowIndex, colIndex) {
            const maxArr = [2, 2, 2, 2, 2]; // 每一行最多有多少可聚焦输入框（从0开始）
            const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
            if (this.$refs[ref][0]) {
                this.$refs[ref][0].focus();
            }
        },
        // detailList粘贴赋值方法
        handlePasteDetailList(event, rowIndex, colIndex) {
            const nameAllArr = this.form.detailList.map((item, index) => {
                return ["chenglhgzzl", "honggqsyhclhzl", "hongghsyhclhzl"]
            });
            const subscriptArr = this.form.detailList.map((item, index) => {
                return [0, 1, 2]
            });
            const methodsAllArr = this.form.detailList.map((item, index) => {
                return ["handlehsl", "handlehsl", "handlehsl"]
            });
            const parameterArr = this.form.detailList.map((item, index) => {
                return [item, item, item]
            });

            const data = handlePaste(event, rowIndex, colIndex, nameAllArr, methodsAllArr, parameterArr, subscriptArr);
            const {cols, nameSetArr, methodsSetArr, parameterSetArr, subscriptSetArr} = data;
            for (let a = 0; a < nameSetArr.length; a++) {
                for (let b = 0; b < nameSetArr[a].length; b++) {
                    this.form.detailList[rowIndex + a][nameSetArr[a][b]] = cols[a][b];
                }
            }
            for (let a = 0; a < methodsSetArr.length; a++) {
                for (let b = 0; b < methodsSetArr[a].length; b++) {
                    if (methodsSetArr[a][b]){
                        this.setCalculate(methodsSetArr[a][b], parameterSetArr[a][b]);
                    }
                }
            }
        },

        // 计算方法调用
        setCalculate(name, a, b, c, d, e) {
            this[name](a, b, c, d, e);
        }
    }
};
</script>

<style scoped lang="scss"></style>
